<?php

class ProducerRequestsController extends AppController {

  var $name = 'ProducerRequests';
  
  var $helpers = array('Html','Text','Javascript','Ajax');

  function send() {
    if ($this->checkSession()) {
      if($this->checkLevel(AUTH_MEM)) {
      } else {
        $this->notAuth();
      }
    } else {
      $this->forceLogin();
    }
    if (!empty($this->data)) {
      if ($this->ProducerRequest->save($this->data)) {
        $text = "http://" . $_SERVER['HTTP_HOST'] . str_replace('/index.php', '', $_SERVER['PHP_SELF']) . '/producerRequests/respond/' . $this->ProducerRequest->getLastInsertId();
      
        $from = $this->ProducerRequest->Member->find('Member.member_id = ' . $this->data['ProducerRequest']['member_id'], 'first_name,last_name,email_address');
        
      	$this->Email->to = 'Producer Request <producerRequest@westmichigancoop.com>';
      	$this->Email->from = $from['Member']['first_name'] . ' ' . $from['Member']['last_name']. ' <' . $from['Member']['email_address'] . '>';
      	$this->Email->subject = "Producer Privs Request: " . $this->data['ProducerRequest']['business_name'];
      	$this->Email->template = 'prodrequest';
      	if (Configure::read('debug')) {
      		$this->Email->delivery = 'debug';
      	}
      	
      	$this->set('swiftMailer_data',$text);
      	
      	if ($this->Email->send()) {
        	$this->Session->setFlash("Producer Request Sent!  Don't forget to send your application to <strong>West Michigan Coop, 1111 Godfrey SW, Suite S250, 49503</strong>.", 'message_ok');
        	$this->redirect('/members/menu');
        } else {
        	$this->set('errorMessage','The mailer failed to send.  Please try again.');
        }
      } else {
        $this->set('errorMessage','There was an error in your form.  Please double check that you have entered everything correctly.');
      }
    }
    //print_r(array($to,$ret,$body));
    $member = $this->ProducerRequest->Member->find('username = "' . $this->userInfo['username'] . '"', 'member_id,email_address');
    $this->set('member',$member);
    $this->render();
  }
  
  function respond($id = null) {
    if ($this->checkSession()) {
      if($this->checkLevel(AUTH_ADMIN)) {
      } else {
        $this->notAuth();
      }
    } else {
      $this->forceLogin();
    }
    if ($id === null) {//if there's no id, they can't respond to an invite.
      $this->Session->setFlash("That operation is not available to you.");
      $this->redirect('/');
    } else {
      $r = $this->ProducerRequest->query("SELECT * FROM producer_requests where id=$id" );

      //print($id);
      if (empty($r)) {
        $this->Session->setFlash("That request has already been responded to.");
        $this->redirect('/');
      } else {
        //display the form
        //print_r($invite);
        $this->data['Producer'] = $r[0]['producer_requests'];
        $this->data['Producer']['producer_id'] = $this->ProducerRequest->Member->Producer->recommendProducerId($r[0]['producer_requests']['business_name']);
        $this->set('request_id',$id);
        //$this->render();
      }
    }
  }
  
  function taken($id = null) {
    if ($id === null) {
      $this->Session->setFlash("That operation is not available to you.");
      $this->redirect('/');
    } else {
    	Configure::write('debug', 2);
      $prod = $this->ProducerRequest->find('first', array('conditions'=>"ProducerRequest.id = $id"));
      if (!empty($prod)) {
      	$to = $this->ProducerRequest->Member->find('Member.member_id = ' . $prod['ProducerRequest']['member_id'],'first_name,last_name,email_address');
      	
      	$text = "http://" . $_SERVER['HTTP_HOST'] . str_replace('/index.php', '', $_SERVER['PHP_SELF']) . '/';
      	
      	$this->Email->to = $to['Member']['first_name'] . ' ' . $to['Member']['last_name']. ' <' . $to['Member']['email_address'] . '>';
      	$this->Email->from = 'WMCoop: Producer Privileges <producerRequest@westmichigancoop.com>';
     		$this->Email->subject = "Producer Privs Request: " . $to['Member']['first_name'] . ' ' . $to['Member']['last_name'];
      	$this->Email->template = 'prodapproved';
      	if (Configure::read('debug')) {
      		$this->Email->delivery = 'debug';
      	}
      	
      	$this->set('swiftMailer_data',$text);
      	
      	if ($this->Email->send()) {
      		$this->ProducerRequest->del($id);
      		$this->Session->setFlash("Producer Request Approved - The producer has been emailed regarding their new status.", 'message_ok');
      	} else {
      		$this->Session->setFlash('The mailer had an error.  Please try again.', 'message_error');
      	}
      } else {
      	$this->Session->setFlash('That request has already been handled.');
      }
      $this->redirect('/members/menu');
    }
  }
  
  function view() {
  	$this->adminOnly();
  	$this->set('requests', $this->ProducerRequest->findAll('Member.auth_type <> "producer"'));
  }
  
}
